package cn.stylefeng.guns.modular.demo.controller;


import cn.stylefeng.guns.core.exception.BusinessException;
import cn.stylefeng.guns.core.exception.BusinessExceptionEnum;
import cn.stylefeng.guns.dao.IGoodsService;
import cn.stylefeng.guns.dao.entity.Goods;
import cn.stylefeng.guns.dao.entity.GoodsGroup;
import cn.stylefeng.guns.dao.entity.GoodsGroupRelation;
import cn.stylefeng.guns.dao.entity.RuleSupplier;
import cn.stylefeng.guns.dao.service.IGoodsGroupRelationService;
import cn.stylefeng.guns.dao.service.IGoodsGroupService;
import cn.stylefeng.guns.modular.demo.controller.domain.GoodsGroupQueryRequest;
import cn.stylefeng.guns.modular.demo.controller.domain.GoodsGroupSaveRequest;
import cn.stylefeng.guns.modular.demo.controller.domain.GoodsQueryRequest;
import cn.stylefeng.guns.modular.demo.controller.domain.VenderVO;
import cn.stylefeng.guns.modular.demo.excel.domain.GoodsExcelVO;
import cn.stylefeng.guns.modular.demo.excel.listener.GoodsListener;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.rule.annotation.BusinessLog;
import cn.stylefeng.roses.kernel.rule.enums.ResBizTypeEnum;
import cn.stylefeng.roses.kernel.rule.pojo.response.ResponseData;
import cn.stylefeng.roses.kernel.rule.pojo.response.SuccessResponseData;
import cn.stylefeng.roses.kernel.scanner.api.annotation.ApiResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.PostResource;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gettyio.core.util.StringUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author quspacedraon
 * @since 2024-01-25
 */
@RestController
@Slf4j
@ApiResource(name = "商品组", resBizType = ResBizTypeEnum.BUSINESS, path = "/goods/group")
public class GoodsGroupController {
    @Resource
    private IGoodsService goodsService;

    @Resource
    private IGoodsGroupService goodsGroupService;
    @Resource
    private IGoodsGroupRelationService goodsGroupRelationService;


    /**
     * 示例方法
     *
     * @author fengshuonan
     * @since 2021/1/24 10:59
     */
    @PostResource(name = "商品组列表", path = "/list", requiredLogin = true, requiredPermission = true)
    @ApiOperation("商品组列表")
    public ResponseData<PageResult<GoodsGroup>> list(@RequestBody GoodsGroupQueryRequest queryRuleRequest) {

        Page<GoodsGroup> page = new Page<>();
        page.setCurrent(queryRuleRequest.getPageNo());
        page.setSize(queryRuleRequest.getPageSize());
        LambdaQueryWrapper<GoodsGroup> lambdaQueryWrapper = Wrappers.<GoodsGroup>lambdaQuery().like(!StringUtils.isEmpty(queryRuleRequest.getGroupName()), GoodsGroup::getGroupName, queryRuleRequest.getGroupName());

        page = goodsGroupService.page(page, lambdaQueryWrapper);
        return new SuccessResponseData<>(PageResultFactory.createPageResult(page));
    }


    @PostResource(name = "商品组列表", path = "/save", requiredLogin = true, requiredPermission = true)
    @ApiOperation("商品组列表")
    public ResponseData<Boolean> save(@RequestBody GoodsGroupSaveRequest goodsGroupSaveRequest) {

        String goodsGroupId = goodsGroupSaveRequest.getGoodsGroupId();
        String groupName = goodsGroupSaveRequest.getGroupName();
        List<String> goodsCodeList = goodsGroupSaveRequest.getGoodsCodeList();

        GoodsGroup goodsGroup = goodsGroupService.getById(goodsGroupId);
        if (Objects.isNull(goodsGroup)) {
            goodsGroup = new GoodsGroup();
            goodsGroup.setGroupName(groupName);
        }
        boolean save = goodsGroupService.save(goodsGroup);
        goodsGroupId = goodsGroup.getGoodsGroupId();
        goodsGroupRelationService.remove(Wrappers.<GoodsGroupRelation>lambdaQuery().eq(GoodsGroupRelation::getGoodsGroupId, goodsGroupId));
        String finalGoodsGroupId = goodsGroupId;
        List<GoodsGroupRelation> goodsGroupRelationList = goodsCodeList.stream().map(goodsCode -> {
            GoodsGroupRelation goodsGroupRelation = new GoodsGroupRelation();
            goodsGroupRelation.setGoodsGroupId(finalGoodsGroupId);
            goodsGroupRelation.setGoodsCode(goodsCode);
            return goodsGroupRelation;
        }).collect(Collectors.toList());
        if (CollectionUtils.isNotEmpty(goodsGroupRelationList)) {
            goodsGroupRelationService.saveBatch(goodsGroupRelationList);
        }
        return new SuccessResponseData<>(true);
    }


    @ResponseBody
    @PostResource(name = "删除商品组", path = "/delete", requiredLogin = true, requiredPermission = true)
    @ApiOperation("删除关联")
    @BusinessLog
    public ResponseData<Boolean> deleteCorrelation(String goodsGroupId) throws IOException {
        boolean b = goodsGroupService.removeById(goodsGroupId);
        goodsGroupRelationService.remove(Wrappers.<GoodsGroupRelation>lambdaQuery().eq(GoodsGroupRelation::getGoodsGroupId, goodsGroupId));

        return new SuccessResponseData<>(true);
    }

}
